<template>
	<view>
		<view class="top_title">
			<!-- <uni-icons type="left" size="30" @click="back"></uni-icons> -->
			<view class="top_name">选择城市</view>
		</view>
		<text>定位城市</text>
		<navigator url="../index/index" open-type="navigateBack" class="dingwei">
			<uni-icons type="location" size="30"></uni-icons>
			<view @click="goPage(quanguo)">{{quanguo}}</view>
		</navigator>
		<view>
			<text>直辖市</text>
			<view class="city_l">
				<view v-for="(item,index) in list" :key="index">
					<view @click="goPage(item)">{{item}}</view>
				</view>
			</view>
		</view>
		<!-- 纵向滚动 -->
		<view>
			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltoupper="upper"
				@scrolltolower="lower" @scroll="scroll">

				<view v-for="(m,n) in citylist" :key="n">
					<view v-if="n>0?citylist[n-1].initials!==m.initials:citylist">{{m.initials}}</view>
					<uni-collapse accordion>
						<uni-collapse-item :title="m.name" class="my_content">
							<view class="content" v-for="(item,index) in m.children" :key="index"
								@click="goPage(item.name)">
								{{item.name}}
							</view>
						</uni-collapse-item>
					</uni-collapse>
				</view>
			</scroll-view>
		</view>
		<!-- 纵向滚动结束-->
	</view>
</template>

<script>
	import {
		getCity
	} from "../../untils/ajxas.js"
	export default {
		data() {
			return {
				quanguo: "全国",
				list: ["北京", "天津", "上海", "重庆"],
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				citylist: [],
			}
		},
		onLoad() {
			// 城市数据
			getCity().then(res => {
				// console.log(res);
				this.citylist = res.data.data
				// console.log(this.citylist);
			})
		},
		methods: {
			// 返回上一页
			back() {
				uni.navigateBack({
					delta: 1 //返回层级数一层
				})
			},
			goPage(item) {
				console.log(item);
				uni.setStorage({
					key: 'storage_key',
					data: item,
					success: function() {
						console.log('success');
						uni.navigateBack({
							delta: 1
						})
					}
				});
			},
			// zong向滚动
			upper: function(e) {
				// console.log(e)
			},
			lower: function(e) {
				// console.log(e)
			},
			scroll: function(e) {
				// console.log(e)
				this.old.scrollTop = e.detail.scrollTop
			},
			goTop: function(e) {
				// 解决view层不同步的问题
				this.scrollTop = this.old.scrollTop
				this.$nextTick(function() {
					this.scrollTop = 0
				});

			}
		}

	}
</script>

<style>
	.top_title {
		display: flex;
	}

	.top_name {
		width: 700rpx;
		font-size: 40rpx;
		font-weight: bold;
		text-align: center;
	}

	.dingwei {
		display: flex;
		justify-content: flex-start;
	}

	.dingwei view {
		height: 80rpx;
		line-height: 50rpx;
	}

	.city_l {
		width: 750rpx;
		padding: 10rpx 20rpx;
		display: flex;
		justify-content: space-between;
		box-sizing: border-box;
	}

	.city_l view {
		padding: 5rpx 15rpx;
		background-color: #dddddd;
		border-radius: 10rpx;
	}

	/* 滚动 */
	.scroll-Y {
		height: 900rpx;
	}

	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
	}

	.scroll-view-item {
		height: 300rpx;
		line-height: 300rpx;
		text-align: center;
		font-size: 36rpx;
	}

	.scroll-view-item_H {
		display: inline-block;
		width: 100%;
		height: 300rpx;
		line-height: 300rpx;
		text-align: center;
		font-size: 36rpx;
	}

	/* 滚动 结束*/
	.content {
		color: black;
		background-color: #dddddd;
		padding: 10rpx;
		display: inline-block;
		margin: 10rpx 20rpx;
		border-radius: 10rpx;
	}
</style>